﻿using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.报表窗体
{
    public partial class 产量统计 : Form
    {
        public 产量统计()
        {
            InitializeComponent();
            uiDatePicker2.Value = DateTime.Now;
            uiDatePicker1.Value = uiDatePicker2.Value.AddDays(-uiDatePicker2.Value.Date.Day);
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            query();
        }
        /// <summary>
        /// 查询
        /// </summary>
        private void query()
        {
            try
            {
                var juans = Connect.dbcontext.Ado.GetDataTable(string.Format("select CustomerName as 客户,SampleName as 品名,ganghao as 缸号,yanse as 颜色,sum(biaoqianmishu*mashu/100) as 合计长度,danwei as 单位 from juanhaotable where rq between '{0}' and '{1}' and CustomerName='{2}' group by danwei,ganghao,SampleName,CustomerName,yanse",
                    uiDatePicker1.Value.Date, uiDatePicker2.Value.Date.AddDays(1), txtkehu.Text));
                //querystring = $"select danjutable.*,danjumingxitable.*,lxr.FullName from danjutable left join lxr on danjutable.ksmc=lxr.mc,danjumingxitable where danjutable.rq between '{ dateEdit1.DateTime.Date }' and '{dateEdit2.DateTime.Date.AddDays(1) }' and danjutable.ksmc like '%{txtksmc.Text.Replace("'", "''")}%' " +
                //      $"and danjumingxitable.bianhao like '%{txtbianhao.Text.Replace("'", "''") }%' " +
                //      $"and danjumingxitable.pingming like '%{txtpingming.Text.Replace("'", "''") }%' " +
                //      $"and danjumingxitable.guige like '%{txtGuige.Text.Replace("'", "''") }%' " +
                //      $"and danjumingxitable.yanse like '%{txtyanse.Text.Replace("'", "''") }%' " +
                //      $"and danjumingxitable.ganghao like '%{txtganghao.Text.Replace("'", "''")}%' " +
                //      $"and danjumingxitable.OrderNum like '%{txtordernum.Text.Replace("'", "''")}%' " +
                //      $"and danjutable.djlx='{DanjuLeiXing.销售出库单}' " +
                //      $"and danjutable.dh=danjumingxitable.danhao and danjutable.dh in (select Fahuodan.dh from Fahuodan where Fahuodan.dh=danjutable.dh)";
                var dajuanzhuang = Connect.dbcontext.Ado.GetDataTable(string.Format("select CustomName as 客户,pingming as 品名,ganghao as 缸号,yanse as 颜色,chengpingmishu as 合计长度,danwei as 单位 from danjumingxitable where rq between '{0}' and '{1}' and CustomName='{2}' and danhao like 'F%' and danhao not in (select Fahuodan.dh from Fahuodan where Fahuodan.dh=danhao)",
                    uiDatePicker1.Value.Date, uiDatePicker2.Value.Date.AddDays(1), txtkehu.Text));
                List<ShowReport> reports = new List<ShowReport>();
                if (juans.Rows.Count > 0)
                {
                    reports.AddRange(JsonConvert.DeserializeObject<List<ShowReport>>(JsonConvert.SerializeObject(juans)));
                }
                if (dajuanzhuang.Rows.Count > 0)
                {
                    reports.AddRange(JsonConvert.DeserializeObject<List<ShowReport>>(JsonConvert.SerializeObject(dajuanzhuang)));
                }
                foreach(var r in reports.Where(x=>x.单位=="码").ToList ())
                {
                    r.合计长度 =(decimal)( (double )r.合计长度 * 0.9144);
                }
                reports.ForEach(x => x.合计长度 = Math.Ceiling(x.合计长度));
                gridControl1.DataSource = reports;
            }
           
            catch (Exception ex)
            {
                Tools.ShowMessageBox.ShowErrorBox(ex.Message);
            }

        }


        private void 产量统计_Load(object sender, EventArgs e)
        {
            query();
        }

        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { MC = txtkehu.Text, ZJC = "" } };
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
            query();
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "生产统计报表");
        }
    }
}
